package com.xh.jf_blog.controller.admin;

import com.xh.jf_blog.bean.Article;
import com.xh.jf_blog.common.util.R;
import com.xh.jf_blog.service.ArticleService;
import com.xh.jf_blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/3/12/012
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("categoryList", categoryService.getList());
        return "admin/article/article-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R page(int page, int limit, Article article) {
        return articleService.getPage(page, limit, article);
    }

    @PostMapping("/status")
    @ResponseBody
    public R status(Article article) {
        articleService.saveOrUpdate(article);
        return R.ok();
    }

    @DeleteMapping("/delete/{articleId}")
    public R delete(@PathVariable int articleId) {
        articleService.deleteById(articleId);
        return R.ok();
    }
}
